home *** CD-ROM | disk | FTP | other *** search
- program Project1;
-
- {$APPTYPE CONSOLE}
-
- uses
- SysUtils,
- AAGraphs in 'AAGraphs.pas';
-
- procedure PreProcess(aSender : TObject; aNodeInx : integer;
- aExtraData : pointer);
- begin
- writeln('PreProcessing node ', aNodeInx);
- end;
-
- procedure PostProcess(aSender : TObject; aNodeInx : integer;
- aExtraData : pointer);
- begin
- writeln('PostProcessing node ', aNodeInx);
- end;
-
- procedure PrintNode(aSender : TObject; aNodeInx : integer;
- aExtraData : pointer);
- begin
- writeln('Node ', aNodeInx);
- end;
-
- const
- EdgeExists = 1;
-
- var
- FMGraph : TaaFullMatrixGraph;
- TMGraph : TaaTriMatrixGraph;
- LLGraph : TaaLinkListGraph;
- dfIter : TaaDepthFirstIterator;
- bfIter : TaaBreadthFirstIterator;
- i, j : integer;
- Edge : longint;
- ToInx : integer;
- HasCycle: boolean;
-
- begin
- try
- FMGraph := TaaFullMatrixGraph.Create(10, false);
- try
- with FMGraph do begin
- Edges[0, 6] := EdgeExists;
- Edges[0, 9] := EdgeExists;
- Edges[9, 8] := EdgeExists;
- Edges[7, 8] := EdgeExists;
- Edges[5, 6] := EdgeExists;
- Edges[5, 4] := EdgeExists;
- Edges[5, 3] := EdgeExists;
- Edges[5, 8] := EdgeExists;
- Edges[5, 7] := EdgeExists;
- Edges[2, 1] := EdgeExists;
- Edges[2, 3] := EdgeExists;
- Edges[2, 4] := EdgeExists;
- Edges[1, 3] := EdgeExists;
- writeln('---full matrix graph');
- writeln(' 0 1 2 3 4 5 6 7 8 9');
- for i := 0 to 9 do begin
- write(i:2);
- for j := 0 to 9 do begin
- if Edges[i, j] <> EdgeNotPresent then
- write(' 1')
- else
- write(' .');
- end;
- write(' ');
- j := 0;
- while GetNodeEdge(i, j, Edge, ToInx) do begin
- write(ToInx:2);
- inc(j);
- end;
- writeln;
- end;
- end;
-
- dfIter := TaaDepthFirstIterator.Create(FMGraph);
- try
- dfIter.OnPreProcess := PreProcess;
- dfIter.OnPostProcess := PostProcess;
- dfIter.Execute(0, HasCycle, nil);
- finally
- dfIter.Free;
- end;
-
- bfIter := TaaBreadthFirstIterator.Create(FMGraph);
- try
- bfIter.OnPreProcess := PreProcess;
- bfIter.OnPostProcess := PostProcess;
- bfIter.Execute(0, nil);
- finally
- bfIter.Free;
- end;
- finally
- FMGraph.Free;
- end;
- readln;
- //
- TMGraph := TaaTriMatrixGraph.Create(10);
- try
- with TMGraph do begin
- Edges[0, 6] := EdgeExists;
- Edges[0, 9] := EdgeExists;
- Edges[9, 8] := EdgeExists;
- Edges[7, 8] := EdgeExists;
- Edges[5, 6] := EdgeExists;
- Edges[5, 4] := EdgeExists;
- Edges[5, 3] := EdgeExists;
- Edges[5, 8] := EdgeExists;
- Edges[5, 7] := EdgeExists;
- Edges[2, 1] := EdgeExists;
- Edges[2, 3] := EdgeExists;
- Edges[2, 4] := EdgeExists;
- Edges[1, 3] := EdgeExists;
- writeln('---triangular matrix graph');
- writeln(' 0 1 2 3 4 5 6 7 8 9');
- for i := 0 to 9 do begin
- write(i:2);
- for j := 0 to 9 do begin
- if Edges[i, j] <> EdgeNotPresent then
- write(' 1')
- else
- write(' .');
- end;
- write(' ');
- j := 0;
- while GetNodeEdge(i, j, Edge, ToInx) do begin
- write(ToInx:2);
- inc(j);
- end;
- writeln;
- end;
- end;
-
- dfIter := TaaDepthFirstIterator.Create(TMGraph);
- try
- dfIter.OnPreProcess := PreProcess;
- dfIter.OnPostProcess := PostProcess;
- dfIter.Execute(0, HasCycle, nil);
- finally
- dfIter.Free;
- end;
-
- bfIter := TaaBreadthFirstIterator.Create(TMGraph);
- try
- bfIter.OnPreProcess := PreProcess;
- bfIter.OnPostProcess := PostProcess;
- bfIter.Execute(0, nil);
- finally
- bfIter.Free;
- end;
- finally
- TMGraph.Free;
- end;
- readln;
- //
- LLGraph := TaaLinkListGraph.Create(10, false);
- try
- with LLGraph do begin
- Edges[0, 6] := EdgeExists;
- Edges[0, 9] := EdgeExists;
- Edges[9, 8] := EdgeExists;
- Edges[7, 8] := EdgeExists;
- Edges[5, 6] := EdgeExists;
- Edges[5, 4] := EdgeExists;
- Edges[5, 3] := EdgeExists;
- Edges[5, 8] := EdgeExists;
- Edges[5, 7] := EdgeExists;
- Edges[2, 1] := EdgeExists;
- Edges[2, 3] := EdgeExists;
- Edges[2, 4] := EdgeExists;
- Edges[1, 3] := EdgeExists;
- writeln('---linked list graph');
- writeln(' 0 1 2 3 4 5 6 7 8 9');
- for i := 0 to 9 do begin
- write(i:2);
- for j := 0 to 9 do begin
- if Edges[i, j] <> EdgeNotPresent then
- write(' 1')
- else
- write(' .');
- end;
- write(' ');
- j := 0;
- while GetNodeEdge(i, j, Edge, ToInx) do begin
- write(ToInx:2);
- inc(j);
- end;
- writeln;
- end;
- end;
-
- dfIter := TaaDepthFirstIterator.Create(LLGraph);
- try
- dfIter.OnPreProcess := PreProcess;
- dfIter.OnPostProcess := PostProcess;
- dfIter.Execute(0, HasCycle, nil);
- finally
- dfIter.Free;
- end;
-
- bfIter := TaaBreadthFirstIterator.Create(LLGraph);
- try
- bfIter.OnPreProcess := PreProcess;
- bfIter.OnPostProcess := PostProcess;
- bfIter.Execute(0, nil);
- bfIter.OnPreProcess := nil;
- bfIter.OnPostProcess := nil;
- bfIter.OnPrintNode := PrintNode;
- if not bfIter.ShortestPath(0, 7, nil) then
- writeln('no path from 0 to 7');
- finally
- bfIter.Free;
- end;
- finally
- LLGraph.Free;
- end;
- readln;
- //
- LLGraph := TaaLinkListGraph.Create(10, true);
- try
- with LLGraph do begin
- Edges[0, 6] := EdgeExists;
- Edges[0, 9] := EdgeExists;
- Edges[9, 8] := EdgeExists;
- Edges[7, 8] := EdgeExists;
- Edges[5, 6] := EdgeExists;
- Edges[5, 4] := EdgeExists;
- Edges[5, 3] := EdgeExists;
- Edges[5, 8] := EdgeExists;
- Edges[5, 7] := EdgeExists;
- Edges[2, 1] := EdgeExists;
- Edges[2, 5] := EdgeExists;
- Edges[2, 4] := EdgeExists;
- Edges[1, 3] := EdgeExists;
- //Edges[3, 2] := EdgeExists; {uncomment to cause cycle}
- writeln('---linked list digraph');
- writeln(' 0 1 2 3 4 5 6 7 8 9');
- for i := 0 to 9 do begin
- write(i:2);
- for j := 0 to 9 do begin
- if Edges[i, j] <> EdgeNotPresent then
- write(' 1')
- else
- write(' .');
- end;
- write(' ');
- j := 0;
- while GetNodeEdge(i, j, Edge, ToInx) do begin
- write(ToInx:2);
- inc(j);
- end;
- writeln;
- end;
- end;
-
- dfIter := TaaDepthFirstIterator.Create(LLGraph);
- try
- dfIter.OnProcessSortedNode := PrintNode;
- dfIter.TopologicalSort(nil);
- finally
- dfIter.Free;
- end;
- finally
- LLGraph.Free;
- end;
-
- readln;
- //
- LLGraph := TaaLinkListGraph.Create(5, true);
- try
- with LLGraph do begin
- Edges[0, 1] := 5;
- Edges[0, 4] := 15;
- Edges[1, 2] := 20;
- Edges[1, 3] := 10;
- Edges[3, 0] := 15;
- Edges[3, 2] := 25;
- Edges[3, 4] := 20;
- writeln('---linked list weighted digraph');
- writeln(' 0 1 2 3 4');
- for i := 0 to 4 do begin
- write(i:2);
- for j := 0 to 4 do begin
- if Edges[i, j] <> EdgeNotPresent then
- write(Edges[i, j]:3)
- else
- write(' .');
- end;
- write(' ');
- j := 0;
- while GetNodeEdge(i, j, Edge, ToInx) do begin
- write(ToInx:2);
- inc(j);
- end;
- writeln;
- end;
- end;
-
- MinSpanningTree(LLGraph, PrintNode, nil);
-
- finally
- LLGraph.Free;
- end;
- readln;
- //
- LLGraph := TaaLinkListGraph.Create(5, true);
- try
- with LLGraph do begin
- Edges[0, 1] := 5;
- Edges[0, 4] := 50;
- Edges[1, 2] := 40;
- Edges[1, 3] := 10;
- Edges[2, 4] := 5;
- Edges[3, 2] := 10;
- Edges[3, 4] := 20;
- Edges[4, 1] := 15;
- writeln('---linked list weighted digraph');
- writeln(' 0 1 2 3 4');
- for i := 0 to 4 do begin
- write(i:2);
- for j := 0 to 4 do begin
- if Edges[i, j] <> EdgeNotPresent then
- write(Edges[i, j]:3)
- else
- write(' .');
- end;
- write(' ');
- j := 0;
- while GetNodeEdge(i, j, Edge, ToInx) do begin
- write(ToInx:2);
- inc(j);
- end;
- writeln;
- end;
- end;
-
- SmallestCostPath(LLGraph, 0, 4, PrintNode, nil);
-
- finally
- LLGraph.Free;
- end;
-
- except
- on E:Exception do
- writeln(E.Message);
- end;
- readln;
- end.
-